package com.region.integral.common_user.level;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.region.integral.common_user.pojo.CLevelEntity;
import com.region.integral.common_user.query.IIntegralQuery;
import com.region.integral.common_user.xml.Regulation;
import com.region.integral.common_user.xml.dto.Level;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.XMLRegulationUtil;
@Component("common_user_IntegralLevel")
public class IntegralLevelImpl extends BaseHibernate implements IIntegralLevel {
	@Resource(name="common_user_IntegralQuery")
	private IIntegralQuery iIntegralQuery;
	@Override
	public LevelBean getIntegralLevel(int business_userid) {
		LevelBean levelBean = new LevelBean();
		if(iIntegralQuery != null){
			int maxIntegral = iIntegralQuery.getMaxIntegral(business_userid);
			Regulation regulation = XMLRegulationUtil.unmarshaller();
			List<Level> levels = regulation.getLevels();
			if(!levels.isEmpty()){
				for(Level l:levels){
					if(maxIntegral>=l.getLow() && maxIntegral<=l.getHigh()){
						String levelCode = l.getLevelCode();
						try {
							CLevelEntity cLevelEntity = getClevelEntity(levelCode);
							levelBean.setLevelCode(cLevelEntity.getLevelCode());
							levelBean.setLevelPic(cLevelEntity.getLevelPic().getBinaryStream());
							levelBean.setLevelNum(cLevelEntity.getLevelNum());
						} catch (IntegralException e) {
							e.printStackTrace();
						} catch (SQLException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		return levelBean;
	}

}
